package copyengine.screenResize
{
	import copyengine.utils.debug.CELog;

	import flash.display.DisplayObject;
	import flash.geom.Point;

	public final class CEUIAutoResizeListenerWarper implements IScreenResizeListener
	{
		private var target:DisplayObject;

		/**
		 *初始时候的X,Y信息
		 * 注意为GlobalePoint，因为Mc有可能嵌套多层，其坐标系和Stage坐标系不同
		 */
		private var defaultGlobalPosPoint:Point;

		/**
		 *用于计算的临时Point
		 */
		private var calculatePoint:Point;

		private var defaultWidth:int;
		private var defaultHeight:int;

		private var xDirResizeWay:int;
		private var yDirResizeWay:int;

		private var xDirScaleWay:int;
		private var yDirScaleWay:int;

		public function CEUIAutoResizeListenerWarper()
		{
			calculatePoint = new Point();
		}

		public function onScreenResize(_currenStageWidth:int,
									   _currentStageHeight:int,
									   _offestWidth:int,
									   _offestHeight:int,
									   _offsetXPercent:Number,
									   _offsetYPercent:Number):void
		{
			if (target && target.parent)
			{
				doOnScreenResize(_currenStageWidth, _currentStageHeight, _offestWidth, _offestHeight, _offsetXPercent, _offsetYPercent);
			}
		}

		private function doOnScreenResize(_currenStageWidth:int,
										  _currentStageHeight:int,
										  _offestWidth:int,
										  _offestHeight:int,
										  _offsetXPercent:Number,
										  _offsetYPercent:Number):void
		{
			//====调整X方向Move
			if (xDirResizeWay == CEScreenResizeManger.RESIZE_MOVE_OFFSET)
			{
				calculatePoint.x = defaultGlobalPosPoint.x + _offestWidth;
				calculatePoint.y = defaultGlobalPosPoint.y;
				target.x = target.parent.globalToLocal(calculatePoint).x;
			}
			else if (xDirResizeWay == CEScreenResizeManger.RESIZE_MOVE_PERCENT)
			{
				calculatePoint.x = defaultGlobalPosPoint.x * _offsetXPercent;
				calculatePoint.y = defaultGlobalPosPoint.y;
				target.x = target.parent.globalToLocal(calculatePoint).x;
			}
			else if (xDirResizeWay == CEScreenResizeManger.RESIZE_NOT_MOVE)
			{
			}
			else
			{
				CELog.err("CEUIAutoResizeListenerWarper->unknow target x dir resize way");
			}
			//====调整Y方向Move  
			if (yDirResizeWay == CEScreenResizeManger.RESIZE_MOVE_OFFSET)
			{
				calculatePoint.x = defaultGlobalPosPoint.x;
				calculatePoint.y = defaultGlobalPosPoint.y + _offestHeight;
				target.y = target.parent.globalToLocal(calculatePoint).y;
			}
			else if (yDirResizeWay == CEScreenResizeManger.RESIZE_MOVE_PERCENT)
			{
				calculatePoint.x = defaultGlobalPosPoint.x;
				calculatePoint.y = defaultGlobalPosPoint.y * _offsetYPercent;
				target.y = target.parent.globalToLocal(calculatePoint).y;
			}
			else if (yDirResizeWay == CEScreenResizeManger.RESIZE_NOT_MOVE)
			{
			}
			else
			{
				CELog.err("CEUIAutoResizeListenerWarper->unknow target y dir resize way");
			}
			//====调整X方向Scale
			if (xDirScaleWay == CEScreenResizeManger.RESIZE_SCALE_OFFEST)
			{
				target.width = defaultWidth + _offestWidth;
			}
			else if (xDirScaleWay == CEScreenResizeManger.RESIZE_SCALE_HALF_OFFSET)
			{
				target.width = defaultWidth + _offestWidth / 2;
			}
			else if (xDirScaleWay == CEScreenResizeManger.RESIZE_SCALE_PERCENT)
			{
				target.width = defaultWidth * _offsetXPercent;
			}
			else if (xDirScaleWay == CEScreenResizeManger.RESIZE_NOT_SCALE)
			{
			}
			else
			{
				CELog.err("CEUIAutoResizeListenerWarper->unknow target x dir scale way");
			}
			//====调整Y方向Scale
			if (yDirScaleWay == CEScreenResizeManger.RESIZE_SCALE_OFFEST)
			{
				target.height = defaultHeight + _offestHeight;
			}
			else if (yDirScaleWay == CEScreenResizeManger.RESIZE_NOT_SCALE)
			{
			}
			else
			{
				CELog.err("CEUIAutoResizeListenerWarper->unknow target y dir scale way");
			}
		}

		public function setTarget(_target:DisplayObject,
								  _xDirResizeWay:int,
								  _yDirResizeWay:int,
								  _xDirScaleWay:int,
								  _yDirScaleWay:int):void
		{
			target = _target;
			calculatePoint.x = target.x;
			calculatePoint.y = target.y;
			defaultGlobalPosPoint = target.parent.localToGlobal(calculatePoint);
			defaultWidth = _target.width;
			defaultHeight = _target.height;
			xDirResizeWay = _xDirResizeWay;
			yDirResizeWay = _yDirResizeWay;
			xDirScaleWay = _xDirScaleWay;
			yDirScaleWay = _yDirScaleWay;
		}

		public function getTarget():DisplayObject
		{
			return target;
		}

	}
}
